import scrapy
from ..items import StockItem

class StockSpider(scrapy.Spider):
    name = 'stock'
    allowed_domains = ['www.cwl.gov.cn']
    start_urls = ['http://www.cwl.gov.cn/kjxx/ssq/kjgg/']

    def parse(self, response):
        table = response.xpath('//div[@class="wqkj ssq"]/div[@class="containerHome"]/div[@class="bgzt"]/table/tbody//tr')
        stock = StockItem()
        id,date,number = [],[],[]
        for item in table:
            num = []
            id.append(item.xpath('.//td[1]/text()').extract())
            date.append(item.xpath('.//td[2]/text()').extract())
            num = item.xpath('.//td[3]//span/text()').extract()
            num.append(item.xpath('.//td[4]//span/text()').extract_first(''))
            number.append(num)
        stock['id'] = id
        stock['date'] = date
        stock['number'] = number
        return stock

